﻿using System;
using System.Collections;
using System.Reflection;
using System.Web.UI;
using VitCode.BaseDataPageCreator;
using VitCode.Castle.DataAccess;
using VitCode.ObjectResolutionHelper;
using VitCode.Web.UI.WebControls;

namespace VitCode.Web.UserControls
{
    public partial class ChildObjectSetCtl : UserControl
    {
        private PageLoadHelper m_LoadHelper;

        public bool Enabled
        {
            get { return txtObjVal.Enabled; }
            set
            {
                txtObjVal.Enabled = value;
                ibtSeach.Visible = value;
            }
        }

        public string InitCurrUser
        {
            get { return hdnInitCurrUser.Value; }
            set 
            {
                hdnInitCurrUser.Value = value;
                if (hdnInitCurrUser.Value == "true")
                {
                    txtObjId.Text = GenericContext.CurrUserId.ToString();
                    txtObjVal.Text = GenericContext.CurrUserName;
                }
            }
        }
        
        public string ObjClassName
        {
            get { return hdnClassName.Value; }
            set { hdnClassName.Value = value; }
        }

        public string ObjTypeName
        {
            get { return hdnTypeName.Value; }
            set { hdnTypeName.Value = value; }
        }

        public string ObjKeyProName
        {
            get { return hdnKeyProName.Value; }
            set { hdnKeyProName.Value = value; }
        }

        public string ObjShowProName
        {
            get { return hdnShowProName.Value; }
            set { hdnShowProName.Value = value; }
        }

        public string ConditionString
        {
            get { return hdnConditionString.Value; }
            set { hdnConditionString.Value = value; }
        }

        public object ObjKeyProVal
        {
            get { return txtObjId.Text; }
            set
            {
                if(value!=null)
                {
                    txtObjId.Text = m_LoadHelper.GetProObjStringValue(ObjKeyProName, value);
                    hdnKeyProVal.Value = txtObjId.Text;
                }
            }
        }

        public object ObjShowProVal
        {
            get { return txtObjVal.Text; }
            set
            {
                if (value != null)
                {
                    txtObjVal.Text = m_LoadHelper.GetProObjStringValue(ObjShowProName, value);
                    hdnShowProVal.Value = txtObjVal.Text;
                } 
            }
        }

        public string OtherCtlContext
        {
            get { return hdnOtherCtlContext.Value; }
            set { hdnOtherCtlContext.Value = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            if (m_LoadHelper == null)
                m_LoadHelper = new PageLoadHelper(BaseDataSettingXmlPath);
            base.OnInit(e);
        }

        private string BaseDataSettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Setting\\BaseDataSet.xml"; }
        }

        private void ShowDialog()
        {
            var strHttpHeadPath = UrlHelper.GetHttpHeadPath();
            if (!strHttpHeadPath.EndsWith("/"))
                strHttpHeadPath += "/";
            string selectPageUrlValue = strHttpHeadPath + "BaseData/SelObjList.aspx";
            var tempWin = new Window(Page, ObjClassName + "選擇頁面")
                              {
                                  Width = 800,
                                  Height = 600,
                                  Scrollbars = true,
                                  Status = true
                              };
            var reValues = new[] { txtObjId.ClientID,txtObjVal.ClientID,hdnKeyProVal.ClientID, hdnShowProVal.ClientID};
            selectPageUrlValue += "?" + BaseDataKeyInfo.CLASS_NAME + "=" + ObjClassName;
            if (ObjTypeName.Length > 0)
                selectPageUrlValue += "&" + BaseDataKeyInfo.TYPE_NAME + "=" + ObjTypeName;
            if (ObjShowProName.Length > 0)
                selectPageUrlValue += "&" + BaseDataKeyInfo.KEY_CONTEXT + "=" + ObjShowProName + ":1,"+ObjKeyProName+":2,"+ObjShowProName+":3";
            if (ObjKeyProName.Length > 0)
                selectPageUrlValue += "&" + BaseDataKeyInfo.KEY_PROPERTY + "=" + ObjKeyProName+":0";
            
            if (OtherCtlContext.Length > 0)
                selectPageUrlValue += "&" + BaseDataKeyInfo.OTHER_CTL_CONTEXT + "=" + OtherCtlContext ;
            if (ConditionString.Length > 0)
            {
                var val = ConditionString;
                Session[selectPageUrlValue + BaseDataKeyInfo.CONDITION_STRING] = val;
            }
            tempWin.ShowModalDialog(selectPageUrlValue, reValues,false);
            //tempWin.Show(selectPageUrlValue);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(txtObjId.Text.Length==0 && hdnKeyProVal.Value.Length>0)
                txtObjId.Text = hdnKeyProVal.Value;
            if(txtObjVal.Text.Length==0 && hdnShowProVal.Value.Length>0)
                txtObjVal.Text = hdnShowProVal.Value;
        }

        protected void ibtSeach_Click(object sender, ImageClickEventArgs e)
        {
            ShowDialog();
        }
    }
}
